#include <iostream>
#include <vector>
#include <queue>

std::vector<int> GetLeastNumbers(const std::vector<int>& arr, int k) {
    std::vector<int> result;
    if (k <= 0 || arr.empty() || k > arr.size()) {
        return result;
    }
 
    std::priority_queue<int, std::vector<int>, std::less<int>> maxHeap;

    for (int i = 0; i < k; ++i) {
        maxHeap.push(arr[i]);
    }
    
    for (int i = k; i < arr.size(); ++i) {
        if (arr[i] < maxHeap.top()) {
            maxHeap.pop();
            maxHeap.push(arr[i]);
        }
    }

    while (!maxHeap.empty()) {
        result.push_back(maxHeap.top());
        maxHeap.pop();
    }
    
    return result;
}

int main() {
    std::vector<int> arr = {4, 5, 1, 6, 2, 7, 3, 8};
    int k = 4;
    std::vector<int> leastNumbers = GetLeastNumbers(arr, k);
    
    std::cout << "The least " << k << " numbers are: ";
    for (auto num : leastNumbers) {
        std::cout << num << " ";
    }
    
    return 0;
}
